package com.kkui.liuxin.controller;


import com.kkui.liuxin.mapper.SelectUsernameMp;
import com.kkui.liuxin.pojo.User;
import com.kkui.liuxin.shiro_jwt.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * @ClassName: TokenIs
 * @Description: 判断token是否合法
 * @author: kkui
 * @date: 2022/4/10 21:32
 */

@Controller
@ResponseBody
@RequestMapping("/api")
public class TokenIs {
    @Resource
    SelectUsernameMp selectUsernameMp;

    @GetMapping("/token")
    public boolean TokenIsLegal(HttpServletRequest request){
        String authorization = request.getHeader("Authorization");
        String username = JwtUtil.getUsername(authorization);
        User user = selectUsernameMp.getUser(username);
        if (user==null){
            return false;
        }

        return JwtUtil.isExpire(authorization) || JwtUtil.verifyToken(authorization,username,user.getSalt());


    }
}
